import 'package:flutter/material.dart';
import 'model.dart';

import '../util.dart';
import 'xf_api_utils.dart';

/// @Author : 王亚东

/// @Time : On 2022/3/2 14:02

class IflyDialog extends StatefulWidget {
  const IflyDialog({
    Key? key,
    required this.man,
  }) : super(key: key);
  final XFApiUtils man;
  @override
  _IflyDialogState createState() => _IflyDialogState();
}

class _IflyDialogState extends State<IflyDialog> {
  String text = '';
  late XFApiUtils man;

  int status = 0;

  @override
  void initState() {
    super.initState();
    man = widget.man;
    man.status = XFRecordStatus.unStart;
    man.onCompleted = () {
      Get.log('录音完成');
      // setStatus(XFRecordStatus.transFinish);
      Get.back(result: RecordResult.success(transform: text));
    };
  }

  @override
  void dispose() {
    super.dispose();
    if (man.status == XFRecordStatus.recording) {
      man.cancel();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: Get.width,
      height: 0.45.sh,
      padding: EdgeInsets.fromLTRB(15.r, 0, 15.r, 0),
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(
              topRight: Radius.circular(30.r), topLeft: Radius.circular(30.r))),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        mainAxisSize: MainAxisSize.min,
        children: [
          Stack(
            alignment: Alignment.topRight,
            children: [
              Center(
                child: Container(
                  decoration: BoxDecoration(
                      color: Colors.grey,
                      borderRadius: BorderRadius.all(Radius.circular(30.r))),
                  width: 100.w,
                  height: 3.h,
                  margin: EdgeInsets.only(top: 10.r, bottom: 0),
                ),
              ),
              Positioned(
                child: IconButton(
                  onPressed: () {
                    man.cancel();
                    Get.back(result: RecordResult.cancel());
                  },
                  iconSize: 30.r,
                  icon: Icon(
                    Icons.close,
                    color: Colors.red,
                    size: 25.r,
                  ),
                ),
              ),
            ],
          ),
          Expanded(
              child: Text(
            text,
            style: TextStyle(fontSize: 16.sp, height: 1.5),
            textAlign: TextAlign.start,
          )),
          Expanded(
            child: LottieBuilder.asset(
              'assets/jsons/voice_loading.json',
              animate: man.status == XFRecordStatus.recording,
              repeat: true,
              reverse: true,
              // width: 200.r,
              width: Get.width - 120.r,
              fit: BoxFit.fill,
            ),
          ),
          const Divider(
            height: 1,
            color: ColorsUtil.separator,
          ),
          SafeArea(
            child: GestureDetector(
                onTap: () async {
                  if (man.status == XFRecordStatus.transFinish) {
                    Get.back(result: RecordResult.success(transform: text));
                  }
                },
                onLongPressStart: (details) {
                  if (man.status == XFRecordStatus.transFinish) return;
                  Get.log('录音开始');
                  setStatus(XFRecordStatus.recording);
                  actionAudio();
                },
                onLongPressEnd: (details) {
                  if (man.status == XFRecordStatus.transFinish) return;
                  Get.log('录音结束');
                  man.stop();
                  setState(() {});
                },
                onLongPressCancel: () {
                  Get.log('手动取消录音');
                  man.cancel();
                  setState(() {});
                },
                child: Container(
                  width: 120.r,
                  height: 40.r,
                  alignment: Alignment.center,
                  margin: EdgeInsets.only(top: 15.r, bottom: 15.r),
                  decoration: BoxDecoration(
                    color: (man.status == XFRecordStatus.recording ||
                            man.status == XFRecordStatus.recordFinish)
                        ? Colors.red.withAlpha(100)
                        : Colors.red,
                    borderRadius: BorderRadius.circular(20.r),
                  ),
                  child: Text(
                    actionText(),
                    style: TextStyle(color: Colors.white, fontSize: 16.sp),
                  ),
                )),
          )
        ],
      ),
    );
  }

  String actionText() {
    switch (man.status) {
      case XFRecordStatus.unStart:
        return '按住说话';
      case XFRecordStatus.recording:
        return '录音中';
      case XFRecordStatus.recordFinish:
        return '准备中';
      case XFRecordStatus.transFinish:
        return '发送';
      case XFRecordStatus.cancel:
        return '按住说话';
    }
  }

  void setStatus(XFRecordStatus status) {
    man.status = status;
    setState(() {});
  }

  void actionAudio() async {
    await man.start((String result) {
      Get.log('---${man.status}---' + result.toString());

      setState(() {
        text = result;
      });
    });
  }
}
